package view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import util.StringUtils;
import dao.questionTypeDao;
import entity.questions;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class addquestion extends JInternalFrame {
	private JTextField questionTxt;
	private JTextField selectA;
	private JTextField selectB;
	private JTextField selectC;
	private JTextField selectD;
	private JTextField correct;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					addquestion frame = new addquestion();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public addquestion() {
		setClosable(true);
		setIconifiable(true);
		setTitle("添加试题");
		setBounds(100, 100, 751, 448);
		
		JLabel lblNewLabel = new JLabel("题干");
		
		questionTxt = new JTextField();
		questionTxt.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("选项A");
		
		JLabel lblNewLabel_2 = new JLabel("选项B");
		
		JLabel lblNewLabel_3 = new JLabel("选项C");
		
		JLabel lblNewLabel_4 = new JLabel("选项D");
		
		selectA = new JTextField();
		selectA.setColumns(10);
		
		selectB = new JTextField();
		selectB.setColumns(10);
		
		selectC = new JTextField();
		selectC.setColumns(10);
		
		selectD = new JTextField();
		selectD.setColumns(10);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加功能
				questionAddAction(e);
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 15));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//重置按钮功能
				questionRestAction(e);
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel lblNewLabel_5 = new JLabel("正确答案");
		
		correct = new JTextField();
		correct.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(47)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_3)
								.addComponent(lblNewLabel_4)
								.addComponent(lblNewLabel_5))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(correct, Alignment.TRAILING)
								.addComponent(selectD)
								.addComponent(selectC)
								.addComponent(selectB)
								.addComponent(selectA)
								.addComponent(questionTxt, GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(225)
							.addComponent(btnNewButton)
							.addGap(148)
							.addComponent(btnNewButton_1)))
					.addContainerGap(130, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
						.addComponent(questionTxt, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(selectA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(8)
							.addComponent(lblNewLabel_2)
							.addGap(22)
							.addComponent(lblNewLabel_3)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblNewLabel_4))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(selectB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(selectC, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(selectD, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_5)
						.addComponent(correct, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addGap(22))
		);
		getContentPane().setLayout(groupLayout);

	}
	//问题添加功能
protected void questionAddAction(ActionEvent e) {
		// TODO Auto-generated method stub
	String question=questionTxt.getText();//题干
	String A=selectA.getText();//选项
	String B=selectA.getText();
	String C=selectA.getText();
	String D=selectA.getText();
	String cor=correct.getText();
	//不能为空
	if(StringUtils.isEmpty(question)) {
		JOptionPane.showMessageDialog(null,"题干不能为空");
		return;
	}
	//真正的添加
	questions qe=new questions(question,A,B,C,D,cor);
	int rs=questionTypeDao.Addquestion(qe);
	if(rs==1) {
		JOptionPane.showMessageDialog(null,"添加成功");
	}else {
		JOptionPane.showMessageDialog(null,"添加失败，请稍后尝试");
	}
	}

	//重置功能
	protected void questionRestAction(ActionEvent e) {
		// TODO Auto-generated method stub
		questionTxt.setText("");//清空
		selectA.setText("");
		selectB.setText("");
		selectC.setText("");
		selectD.setText("");
		correct.setText("");
	}
}